package com.liu.lab03.Servlet;

import com.liu.lab03.dao.MusicDao;
import com.liu.lab03.pojo.Music;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "addMusicServlet", value = "/addMusic")
public class addMusicServletServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//            收集音乐信息
             // 刘小文  -2420710103---
            String musicname = request.getParameter("musicname");
            String singer = request.getParameter("singer");
            int ranking = Integer.parseInt(request.getParameter("ranking"));
            // 使用我们当且的自定义类去打包数据
            Music music = new Music();
            music.setMusicname(musicname);
            music.setSinger(singer);
            music.setRanking(ranking);

            System.out.println(ranking);
            //调用我们 servlet 的方法访问数据库
            MusicDao dao = new MusicDao();
            int rows = dao.addMusic(music);


            // 根据数据库的返回值 显示选择结果
            if(rows>0){
                response.sendRedirect("showAllMusics");
            }else{
                PrintWriter out = response.getWriter();
                out.println("添加失败！ <a href='showAllMusics'>返回</a>");
            }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
